﻿(function ($) {
    var renderMode = {
        block: 0,
        inline: 1
    };

    var methods = {
        init: function (options) {
            this.each(function () {
                var data = $(this).data("updatePanel-data");

                if (!data) {
                    data = {
                        updatePanel: $(this),
                        options: options
                    };
                }

                if (options.triggers && options.triggers.length > 0) {
                    $.each(options.triggers, function (index, trigger) {
                        var triggerElement = $("#" + trigger.name);
                        var triggerData = triggerElement.data("updatePanel-data");

                        if (!triggerData) {
                            var triggerData = {
                                updatePanels: [],
                                trigger: trigger
                            };
                        }

                        triggerData.updatePanels.push(data);

                        if (triggerElement.prop("tagName") == "INPUT") {
                            switch (triggerElement.attr("type")) {
                                case "text", "password", "checkbox":
                                    triggerElement.change(methods.triggerThrown);
                                    break;
                                default:
                                    triggerElement.click(methods.triggerThrown);
                                    break;
                            }
                        }
                        else if (triggerElement.prop("tagName") == "SELECT" ||
                            triggerElement.prop("tagName") == "TEXTAREA") {
                            triggerElement.change(methods.triggerThrown);
                        }

                        triggerElement.data("updatePanel-data", triggerData);
                    });
                }

                $(this).data("updatePanel-data", data);
            });
        },
        destroy: function () {
        },
        triggerThrown: function () {
            var data = $(this).data("updatePanel-data");
            var updatePanelsToUpgrade = $("#__UPDATEPANELSTOUPGRADE");

            if (data.trigger.async) {
                var scriptManagerData = $(window).data("scriptManager-data");

                $.each(data.updatePanels, function (index, item) {
                    if (updatePanelsToUpgrade.val() != "") {
                        updatePanelsToUpgrade.val(updatePanelsToUpgrade.val() + '|' + item.updatePanel.attr("id"));
                    }
                    else {
                        updatePanelsToUpgrade.val(item.updatePanel.attr("id"));
                    }
                });

                scriptManagerData.scriptManager.pageRequestManager("doPostBack", $(this).attr("name"), "");

                return false;
            }

            return true;
        }
    };

    $.fn.updatePanel = function (method, options) {
        var settings = $.extend({
            renderMode: renderMode.block,
            triggers: null
        });

        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.updatePanel');
        }
    };
})(jQuery);